var util = require('./util.js')
var api = util.config().api //url
let token = uni.getStorageSync('y-token');
let userId = uni.getStorageSync('y-userId');
let loadingTime = 500 //自定义loading时间
var request = new Object()
// 前端请求接口  get
request.getRequest = function(obj) {
	uni.showLoading({
		title: "挖掘中...",
		mask: true,
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: "/dev" + obj.url,
			data: obj.data,
			method: "GET",
			header: {
				'userId': userId,
				"Pragma": "no-cache",
				'content-type': "application/json"
			},
			success: function(res) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)
				if (res.data.code === 401) {
					uni.showToast({
						title: '暂无权限!',
						icon: "none"
					})
					reject(res)
				} else if (res.data.code === 200) {
					resolve(res)
				} else if (!!res.data.msg && res.data.code != 200) {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					reject(res)
				}
			},
			fail: function(err) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)
				uni.showToast({
					title: err.data.msg,
					icon: "none",
				})
				reject(err)
			}
		})
	})
}

// 前端请求接口  post
request.postRequest = function(obj) {
	uni.showLoading({
		title: "挖掘中...",
		mask: true,
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: "/dev" + obj.url,
			data: obj.data,
			method: "POST",
			header: {
				"Pragma": "no-cache",
				'content-type': "application/json"
			},
			success: function(res) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)
				if (res.data.code === 401) {
					uni.showToast({
						title: '暂无权限!',
						icon: "none"
					})
					reject(res)
				} else if (res.data.code === 200) {
					resolve(res)
				} else if (!!res.data.msg && res.data.code != 200) {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					reject(res)
				}
			},
			fail: function(err) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)

				uni.showToast({
					title: err.data.msg,
					icon: "none"
				})
				reject(err)
			}
		})
	})
}

// 前端请求接口  put
request.putRequest = function(obj) {
	uni.showLoading({
		title: "挖掘中...",
		mask: true,
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: "/dev" + obj.url,
			data: obj.data,
			method: "PUT",
			header: {
				"Pragma": "no-cache",
				'content-type': "application/json"
			},
			success: function(res) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)
				if (res.data.code === 401) {
					uni.showToast({
						title: '暂无权限!',
						icon: "none"
					})
					reject(res)
				} else if (res.data.code === 200) {
					resolve(res)
				} else if (!!res.data.msg && res.data.code != 200) {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					reject(res)
				}
			},
			fail: function(err) {
				setTimeout(() => {
					uni.hideLoading()
				}, loadingTime)

				uni.showToast({
					title: err.data.msg,
					icon: "none"
				})
				reject(err)
			}
		})
	})
}




module.exports = request
